package com.songxh.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.songxh.common.BaseContext;
import com.songxh.common.R;
import com.songxh.pojo.ShoppingCart;
import com.songxh.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigInteger;
import java.time.LocalDateTime;
import java.util.List;


/**
 * @author songxh
 * @create 2022-04-27-17:59
 */
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;


    /**
     * 添加菜品或者套餐
     * @param shoppingCart
     * @return
     */
    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart) {

        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);

        //查询当前菜品是否在购物车
        Long dishId = shoppingCart.getDishId();

        QueryWrapper<ShoppingCart> queryWrapper = new QueryWrapper<>();
        //找到该用户的购物车
        queryWrapper.eq("user_id", userId);

        if (dishId != null) {
            //添加购物车的是菜品
            queryWrapper.eq("dish_id", dishId);
        } else {
            //添加到购物车的是套餐
            Long setmealId = shoppingCart.getSetmealId();
            queryWrapper.eq("setmeal_id", setmealId);
        }

        //根据dishId或者setmealId获取一个购物数据
        ShoppingCart serviceOne = shoppingCartService.getOne(queryWrapper);

        //判断是否存在
        if (serviceOne != null) {
            //存在
            Integer number = serviceOne.getNumber();
            serviceOne.setNumber(number + 1);
            shoppingCartService.updateById(serviceOne);
        } else {
            //不存在
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCart.setNumber(1);
            shoppingCartService.save(shoppingCart);
            serviceOne = shoppingCart;
        }

        return R.success(serviceOne);
    }


    /**
     * 购物车的商品的减少功能
     * @param shoppingCart
     * @return
     */
    @PostMapping("/sub")
    public R<ShoppingCart> sub(@RequestBody ShoppingCart shoppingCart){
        //客户端传来的dishId
        Long dishId = shoppingCart.getDishId();
        QueryWrapper<ShoppingCart> queryWrapper=new QueryWrapper<>();
        if (dishId!=null){
            //减去的是菜
            queryWrapper.eq("dish_id",dishId);
        }else {
            //减去的是套餐
            Long setmealId = shoppingCart.getSetmealId();
            queryWrapper.eq("setmeal_id",setmealId);
        }

        //根据客户端传来的数据查询该菜品
        ShoppingCart cart = shoppingCartService.getOne(queryWrapper);
        Integer number = cart.getNumber();
        //如果该菜品只有一分，便将该菜品删除
        if (number==1){
            shoppingCartService.remove(queryWrapper);
            //因为需要返回一个shoopcar对象，所有创建一个number为0的空对象来回值给客户端
            ShoppingCart sc = new ShoppingCart();
            sc.setNumber(0);
            cart=sc;
        }else {
            cart.setNumber(number-1);
            shoppingCartService.updateById(cart);
        }

        return R.success(cart);
    }


    /**
     * 显示购物车内容
     * @return
     */
    @GetMapping("/list")
    public R<List<ShoppingCart>> list(){

        //根据用户id来查询当前购物车的内容
        QueryWrapper<ShoppingCart> queryWrapper=new QueryWrapper<>();
        Long id = BaseContext.getCurrentId();
        queryWrapper.eq("user_id",id);
        queryWrapper.orderByAsc("create_time");
        List<ShoppingCart> list = shoppingCartService.list(queryWrapper);

        return R.success(list);
    }


    /**
     * 清空购物车
     * @return
     */
    @DeleteMapping("/clean")
    public R<String> clean(){

        //根据用户id来删除菜品
        Long id = BaseContext.getCurrentId();
        QueryWrapper<ShoppingCart> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("user_id",id);
        shoppingCartService.remove(queryWrapper);

        return R.success("清空成功");
    }


}
